home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
439_01
/
dontread.me
< prev
next >
Wrap
Text File
|
1995-02-10
|
52KB
|
886 lines
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+ +
;+ DONTREAD.ME Version 2.0 +
;+ A Tutorial for XLIB and Protected Mode +
;+ +
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; If you don't read much, then this file was made just for you. It's
;a lot shorter than XLIB.DOC. It also explains several protected-mode
;concepts, including some that are needed to understand XLIB.DOC. We work
;through an assembly language program which demonstrates the usage of XLIB.
;As we go, we are going to do a lot of talking about protected mode in
;general as well as protected mode under XLIB in particular.
; First, let's talk about what XLIB is generally designed to do: XLIB
;will allow you to write protected-mode procedures using your familiar
;language development tools (compilers, linkers, etc), and will allow you to
;execute code containing such procedures using DOS. XLIB is also designed
;to make all this as simple for you as it possibly can. That turns out to
;very simple indeed.
; XLIB has one major shortcoming: It relies on DOS to load executables
;and files. This means that our code must reside in the first meg because
;that's the only thing DOS knows how to work with. It also means that when
;we transfer disk files to or from extended memory, we must perform a
;transfer through a buffer in the first meg. In almost every other regard,
;we will have unlimited power. Most importantly, we will be able to access
;data in extended memory with extreme ease and speed.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Now, let's start with a little program. We must begin with a model
;declaration. XLIB is a large-model library which uses PASCAL conventions.
;We will use the same model here; however, this is not a requirement of XLIB.
;We will use the FARSTACK option because this ensures that our real-mode
;stack will be at the end of the program. This is where it should be under
;most circumstances anyway, but TASM doesn't always put it there (see the
;section on bugs in README.DOC). FARSTACK allows us to conclude that the
;initial SS:SP points to the end of the program. This will become useful
;information a few lines down. We also want to tell the assembler to let us
;use 32-bit instructions. We do this with the .386 directive.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.MODEL LARGE,PASCAL,FARSTACK
.386
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; The next two line are the key ingredients. We want to provide XLIB
;symbols to this program with XLIB.INC. We also want to link with XLIBE.LIB.
;XLIBE.LIB has exception trapping capabilities whereas XLIB.LIB does not.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INCLUDE XLIB.INC
INCLUDELIB XLIBE.LIB
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;TASM Only:
;
; If you are a TASM programmer, then don't use the last two lines. Use the
;following lines instead. These lines include some useful macros that should
;have been in TASM to begin with.
;
; MASM51 ;Emulate MASM51
; QUIRKS ;MASM51 quirks are sometimes nice
;
;PUSHW MACRO IMMEDIATE16:REST ;PUSH 16-bit constant
; IF (@WordSize EQ 4)
; DB 66H
; ENDIF
; DB 68H
; DW IMMEDIATE16
; ENDM
;
;PUSHD MACRO IMMEDIATE32:REST ;PUSH 32-bit constant
; IF (@WordSize EQ 2)
; DB 66H
; ENDIF
; DB 68H
; DD IMMEDIATE32
; ENDM
;
; INCLUDE XLIBB.INC
; INCLUDELIB XLIBEB.LIB
;
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Now we will finish our simplified segment directives.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.STACK 1024
.DATA
.CODE
.STARTUP
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Now we have to deal with an annoying complication. XLIBE.LIB might need
;to allocate some conventional memory for its own use. The problem is that
;our program has likely claimed all available memory, even though it isn't
;going to use it. If you are a MASM programmer, then the solution is simple:
;Link with the CPARM:1 parameter. If you are a TASM programmer, then you
;must resize the memory block in which this program is contained. The
;following code will do the trick. This code is also contained in the file
;RESIZE.INC.
;
; MOV AX,SP ;SS:SP = end of program
; SHR AX,4
; MOV BX,SS
; ADD BX,AX
; INC BX ;BX = first para. beyond program
; MOV AX,ES ;ES:0000 = first para. of program
; SUB BX,AX ;BX = program size in para.
; MOV AH,4AH ;Function to resize memory block
; INT 21H ;Carry will be set if failure
; JNC STILLGOING
; MOV AX,4C01H ;Failed to resize so terminate
; INT 21H
;STILLGOING:
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; One more step and we will be ready to hit protected mode!!! We must
;initialize the library by calling INITXLIB. This procedure will return an
;error code in EAX. A code of zero always means success under XLIBE.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CALL INITXLIB
OR EAX,EAX
JZ GOTPOWER
MOV AX,4C01H ;DOS termination function
INT 21H ;You will have to read after all.
;If you simply can't stand user
;manuals, then try throwing out
;some device drivers and TSRs.
GOTPOWER:
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; We don't want to mess around in real mode anymore. As you will shortly
;see, we can do about anything in protected mode that we could do in real
;mode, plus a whole lot more. Moreover, XLIBE makes protected mode easier
;than real mode. We will spend the rest of our time working from a 32-bit
;protected-mode subroutine called PMMAIN. We will get there with an XLIBE
;procedure called CALLPM. Just push the offset of PMMAIN on the stack and
;call CALLPM.
; There is one big assumption involved here: It is assumed that PMMAIN and
;all other 32-bit routines are contained in a segment called TSEG. Don't
;worry, TSEG can be larger than 64K if you live by the rules. Read the
;manual if you want to know what they are.
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PUSHD OFFSET PMMAIN ;Must use a 32-bit offset